<template>
	<view class="detail">
		<view class="detail-title">
			{{formDate.title}}
		</view>
		<view class="detail-header">
			<view class="detail-header-logo">
				<image :src="formDate.author.avatar" mode="aspectFill"></image>
			</view>
			<view class="detail-header-content">
				<view class="detail-header-content_title">
					{{formDate.author.author_name}}
				</view>
				<view class="detail-header-content_info">
					<text>{{formDate.create_time}}</text>
					<text>{{formDate.browse_count}}浏览</text>
					<text>{{formDate.thumbs_up_count}}赞</text>
				</view>
			</view>
		</view>
		<view class="detail-content">
			<view class="detail-html">
				<u-parse :content="formDate.content" :noData="noData"></u-parse>
			</view>
			<view v-if="formDate.content" class="detail-comment">
				<view class="comment-title">最新评论</view>
				<view class="comment-content" v-for="item in commentsList" :key="item.comment_id">
					<comments-box :comments="item" @reply="reply"></comments-box>					 
				</view>
			</view>
		</view>
		<view class="detail-bottom">
			<view class="detail-bottom_input" @click="openComment">
				<text>谈谈你的看法</text>
				<uni-icons type="compose" size="16" color="#f07373"></uni-icons>
			</view>
			<view class="detail-bottom_icons">
				<view class="detail-bottom_icons-box">
					<uni-icons type="chat" size="22" color="#f07373"></uni-icons>
				</view>
				<view class="detail-bottom_icons-box">
					<uni-icons size="22" color="#f07373" :type="is_like?'heart-filled':'heart'"></uni-icons>
					 
				</view>
				<view class="detail-bottom_icons-box">
					<uni-icons type="hand-thumbsup" size="22" color="#f07373"></uni-icons>
				</view>
			</view>
		</view>
		<uni-popup type="bottom" ref="popup" :maskClick="false">
			<view class="popup-wrap">
				<view class="popup-header">
					<text class="popup-header-item" @click="close">取消</text>
					<text class="popup-header-item" @click="submit">发布</text>
				</view>
				<view class="popup-content">
					<textarea  class="popup-textarea" fixed v-model="commentsValue"  placeholder="请输入评论内容" maxlength="200" />
					<view class="popup-count">
						{{commentsValue.length}}/200
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uParse from '@/components/feng-parse/parse'

	export default {
		components: {
			uParse
		},
		data() {
			return {
				formDate: {},
				noData: '<p style="text-align:center;color:#666" >详情记载中...</p>',
				commentsValue:'',		//输入框的值
				commentsList:[],   		//评论列表
				replyformDate:{},		//回复列表
				is_author_like: false,
				is_like: false,
				is_thumbs_up: false
			}
		},
		onLoad(requestQuerystring) {
			this.formDate = JSON.parse(requestQuerystring.params)
			this.get_detail()
			this.get_comments()
			
		},		 
		methods: {
			
			//弹出评论发布窗口
			openComment(){
				this.$refs.popup.open()
			},
			//关闭评论发布窗口
			close(){
				this.$refs.popup.close()
				//this.commentsValue=''
			},
			//发布评论
			submit(){
				if(this.commentsValue.length===0){
					uni.showToast({
						title:"请输入评论内容",
						icon:"none"
					})
					return
				}
				this.update_comment({
					content:this.commentsValue,
					...this.replyformDate
				}) 
				 
			},
			reply(e){
				this.replyformDate={
					comment_id:e.comments.comment_id,
					is_reply:e.is_reply
				}
				if(e.comments.reply_id){
					this.replyformDate.reply_id=e.comments.reply_id
				}
				this.openComment()
			},
			//评论方法
			update_comment(content){
				const formdata={
					article_id:this.formDate._id,
					...content
				} 
				uni.showLoading()
				this.$api.update_comment(formdata).then((res)=>{					
					uni.hideLoading()
					uni.showToast({
						title:"发布评论成功" 
					})
					this.get_comments()
					this.close()
					this.replyformDate={}
					this.commentsValue=''
				})
			},
			//获取文章详情
			get_detail() {
				this.$api.get_detail({
					article_id: this.formDate._id
				}).then((res) => {
					const {
						data
					} = res
					this.formDate = data
					this.is_like = data.is_like 
				})
			},
			//获取评论内容
			get_comments(){
				this.$api.get_comments({
					article_id:this.formDate._id
				}).then((res)=>{
					const{
						data
						}=res
					this.commentsList=data	
					
				})
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		padding: 15px 0;
		padding-bottom: 54px;

	}

	.detail-title {
		padding: 0 15px;
		font-size: 14px;
		font-weight: bold;
		color: #333;
	}

	.detail-header {
		display: flex;
		align-items: center;
		padding: 0 15px;
		margin-top: 10px;

		.detail-header-logo {
			flex-shrink: 0;
			height: 40px;
			width: 40px;
			border-radius: 50%;
			overflow: hidden;

			image {
				height: 100%;
				width: 100%;
			}
		}

		.detail-header-content {
			padding-left: 10px;
			width: 100%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			font-size: 12px;

			.detail-header-content_title {
				font-size: 14px;
				color: #333;
			}

			.detail-header-content_info {
				color: #999;

				text {
					margin-right: 10px;
				}
			}
		}
	}

	.detail-content {
		margin-top: 10px;
		min-height: 500px;

		.detail-html {
			padding: 0 15px;
		}
		.detail-comment{
			margin-top: 30px;
			.comment-title{
				padding: 10px 15px;
				font-size: 14px;
				color: #666;
				border-bottom: 1px #f5f5f5 solid;
			}
			.comment-content{
				padding: 0 15px;
				border-top: 1px #eee solid;
			}
		}

	}

	.detail-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		width: 100%;
		height: 44px;
		border-top: 1px #f5f5f5 solid;
		background-color: #fff;
		box-sizing: border-box;

		.detail-bottom_input {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-left: 10px;
			padding: 0 10px;
			width: 100%;
			height: 30px;
			border: 1px red solid;
			border-radius: 5px;

			text {
				font-size: 14px;
				color: #999;
			}
		}

		.detail-bottom_icons {
			display: flex;
			flex-shrink: 0;
			padding: 0 10px;

			.detail-bottom_icons-box {
				position: relative;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 40px;
			}
		}

	}
	
	.popup-wrap{
		background-color: #fff;
		.popup-header{
			display: flex;
			justify-content: space-between;
			font-size: 14px;
			color: #666;
			border-bottom: 1px #F5F5F5 solid;			 
			.popup-header-item{
				height: 50px;
				line-height: 50px;
				padding: 0 15px; 
			}
		}
		.popup-content{
			width: 100%;
			padding: 15px;
			box-sizing: border-box;
			.popup-textarea{
				width: 100%;
				height: 200px;
			}
			.popup-count{
				display: flex;
				justify-content: flex-end;
				font-size: 12px;
				color: #999;
			}
		}
	}
</style>
